﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.BOOKS
{
    public partial class SRV_BOOKS : System.Web.UI.Page
    {
        private SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1103) == true)
                {
                    if (this.Page.IsPostBack)
                    {
                        DropDownList ddl = ((DropDownList)(CampusPackage1.FindControl("ddlGrade")));
                        ddl.AutoPostBack = true;
                        ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                        // FillControls();
                        lblResult.Text = "";
                        lblResult.ForeColor = Color.Green;

                    }
                    else
                        lblResult.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillControls();
        }
        protected void btnSaveStation_Click(object sender, EventArgs e)
        {

            if (CampusPackage1.CampusId != "0")
            {
                long? Cer_Id = null;
                if (long.Parse(CampusPackage1.CertificateId) != 0)
                    Cer_Id = long.Parse(CampusPackage1.CertificateId);
                long? grade_id = null;
                if (long.Parse(CampusPackage1.GradeId) != 0)
                    grade_id = long.Parse(CampusPackage1.GradeId);
                long? StageId = null;
                if (long.Parse(CampusPackage1.GradeId) != 0)
                    StageId = long.Parse(CampusPackage1.StageId);
                if (btnSaveStation.Text == "حفظ" && !string.IsNullOrEmpty(HFBookId.Value))
                {
                    if (School.Service.BOOKS.SRV_BOOKS.BooksUpdate(new School.Entities.SRV_Book()
                    {
                        Id = long.Parse(HFBookId.Value),
                        Name = txtName.Text,
                        Description = txtDescrptioin.Text,
                        Code = txtCode.Text,
                        Campus_Id = long.Parse(CampusPackage1.CampusId),
                        Cer_Id = Cer_Id,
                        Grad_Id = grade_id,
                        Stage_Id = StageId
                    }))
                    {
                        lblResult.ForeColor = Color.Green;
                        lblResult.Text = "تم الحفظ بنجاح";
                        FillControls();
                        ClearForm();
                    }
                }
                else
                {
                    foreach (SRV_Book item in db.SRV_Book)
                    {
                        if (txtCode.Text.TrimEnd() == item.Code.TrimEnd())
                        {
                            lblResult.ForeColor = Color.Red;
                            lblResult.Text = "هذا الكود مستخدم";
                            return;
                        }
                    }
                    long bookid = 0;
                    if (School.Service.BOOKS.SRV_BOOKS.BooksInsert(new School.Entities.SRV_Book()
                      {
                          Campus_Id = long.Parse(CampusPackage1.CampusId),
                          Code = txtCode.Text,
                          Description = txtDescrptioin.Text,
                          Name = txtName.Text,
                          Cer_Id = Cer_Id,
                          Grad_Id = grade_id,
                          Stage_Id = StageId,
                          BookQty = 0

                      }, out bookid))
                    {
                        lblResult.ForeColor = Color.Green;
                        lblResult.Text = "لقد تمت عمليه الاضافه بنجاح";
                        lblResult.Visible = true;
                        FillControls();
                        ClearForm();
                    }
                    else
                    {
                        lblResult.ForeColor = Color.Red;
                        lblResult.Text = "لم تتم عمليه الاضافه بنجاح من فضلك تاكد من القيم المدخله";
                    }
                }
            }
            else
            {
                lblResult.Visible = true;
            }
        }
        private void ClearForm()
        {
            txtCode.Text = "";
            txtDescrptioin.Text = "";
            txtName.Text = "";
            btnSaveStation.Text = "اضافه";
        }
        private void FillForm(int id)
        {
            SRV_Book MyBook = School.Service.BOOKS.SRV_BOOKS.GetById(id).First();
            txtCode.Text = MyBook.Code;
            txtDescrptioin.Text = MyBook.Description;
            txtName.Text = MyBook.Name;
            CampusPackage1.CampusId = MyBook.Campus_Id.ToString();
            if (MyBook.Cer_Id != null)
                CampusPackage1.CertificateId = MyBook.Cer_Id.ToString();
            if (MyBook.Stage_Id != null)
                CampusPackage1.StageId = MyBook.Stage_Id.ToString();
            if (MyBook.Grad_Id != null)
                CampusPackage1.GradeId = MyBook.Grad_Id.ToString();
       }
        public void FillControls()
        {
            if (CampusPackage1.CampusId != "0" && CampusPackage1.CertificateId != "0" &&
                CampusPackage1.StageId != "0" && CampusPackage1.GradeId != "0")
            {
                gvData.DataSource = School.Service.BOOKS.SRV_BOOKS.getAllGrd(long.Parse(CampusPackage1.CampusId), long.Parse(CampusPackage1.CertificateId)
                    , long.Parse(CampusPackage1.StageId), long.Parse(CampusPackage1.GradeId));
                gvData.DataBind();
            }
        }
        protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (!(e.CommandSource is System.Web.UI.WebControls.GridView))
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                DataKey BookId = gvData.DataKeys[selectedRow.RowIndex];
                if (e.CommandName == "DoEdit")
                {
                    FillForm(int.Parse(BookId.Value.ToString()));
                    btnSaveStation.Text = "حفظ";
                    HFBookId.Value = BookId.Value.ToString();
                }
                else if (e.CommandName == "DoDelete")
                {
                    if (School.Service.BOOKS.SRV_BOOKS.BookDelete(long.Parse(BookId.Value.ToString())))
                    {
                        FillControls();
                        lblResult.ForeColor = Color.Green;
                        lblResult.Text = "تمت عمليه الحذف بنجاح ";
                        lblResult.Visible = true;
                        ClearForm();
                    }
                    else
                    {
                        lblResult.ForeColor = Color.Red;
                        lblResult.Text = "هذا الكتاب لايمكن حذفه حيث انه متعلق ببيانات اخرى";
                    }
                }
            }
          }

        protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvData.PageIndex = e.NewPageIndex;
            FillControls();
        }
   }
}